<?php

// 1. 退出操作后，清除自动登录令牌
$isLogout = false;
if (isset($_SESSION['logout'])&&$_SESSION['logout']===true) {
  $isLogout = true;
  setcookie('remember-token','',time() - 3600);
  unset($_SESSION['logout']);
}
// 2. 判断当前用户是否登录
$isSignin = isset($_SESSION['user'])? true : false;
$user = isset($_SESSION['user'])? $_SESSION['user'] : ['id'=>'-1','user_name' => '游客','password'=>''];

// 3. 自动登录
if (isset($_COOKIE['remember-token']) && !empty($_COOKIE['remember-token']) && !$isLogout && !$isSignin) {
  $rememberToken = $_COOKIE['remember-token'];
  $sql = 'select id,user_name,password from users where remember_token=?';
  $data = query($sql,[$rememberToken]);
  $user = $data[0];
  $_SESSION['user'] = $user;
}
// 4. 取出一次性的错误包信息
$hasError = isset($_SESSION['has_error'])? $_SESSION['has_error'] : false;
$errorBag = isset($_SESSION['error_bag'])? $_SESSION['error_bag'] : ['error' => ''];
// 5. 清除session中的一次性错误信息
if (isset($_SESSION['has_error']) && isset($_SESSION['error_bag'])) {
   unset($_SESSION['has_error']);
   unset($_SESSION['error_bag']);
}

// 6. 判断用户登录时是否勾选了记住我

if (isset($_SESSION['remember-token']) && !empty($_SESSION['remember-token'])) {
    $rememberToken = $_SESSION['remember-token'];
    unset($_SESSION['remember-token']);
    // 把remember-token写入cookie
    setcookie('remember-token', $rememberToken, time() + 3600);

}

$sql = 'select * from messages order by created_at desc';
$data = query($sql);

?>
<!DOCTYPE html>
<html>  
<head>
  <meta charset="utf-8">
  <title>留言板</title>
  <link rel="stylesheet" type="text/css" href="<?php echo assets('css/index.css') ?>">
  </style>
</head>
<body>
  <div id="input">
  	  <div class="tou">
       <a class="username" href="#"><?=$user['user_name'] ?></a>
      </div>
      <?php if($isSignin): ?>
        <form method="post" action="<?php echo url('logout_action') ?>">
          <input class="quit" style="outline: none;border: none;" type="submit" value="退出" />
        </form>
      <?php else: ?>  
        <a href="<?php echo url('signin') ?>" class="quit">登录</a> 
      <?php endif; ?>
  	  <div class="message">
      <form method="post" action="<?php echo url('post_message') ?>">
      <textarea class="word" type="text" name="content" placeholder="请输入留言内容&hellip;"></textarea>
      <div style="font-size:12px;text-align:center;color: #c00;">
        <?php if($hasError) echo $errorBag['error']; ?>
      </div>
      <button class="push" type="submit">留言</button>
      </form>
      </div>
  </div>
  <div id="message">
    <?php foreach ($data as $row): ?> 
  	<div class="text">
  	  <div class="tu"></div>
  	  <div class="name"><?=$row['user_name'] ?>:</div>
  	  <div class="say"><?=$row['content'] ?></div>
  	  <div class="zang"><a href="<?php echo url('like_action') ?>?id=<?=$row['id'] ?>">赞</a> (<?=$row['like_num'] ?>)</div>
      <div class="return">回复</div>
      <div class="delete">
        <?php if ($user['user_name']==$row['user_name']): ?>
          <a href="<?php echo url('delete_message') ?>?id=<?=$row['id'] ?>">删除</a>
        <?php endif; ?>
        </form>
      </div>
      <div class="time"><?=$row['created_at'] ?></div>
  	</div>
    <?php endforeach; ?>
  </div>
  <div id="clear"></div>
</body>
</html>